package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.cloud.user.req.resource.*;
import com.dd.cloud.user.res.resource.*;
import com.dd.cloud.user.service.resource.IAdvertInfoService;
import com.dd.cloud.user.service.resource.IPostInfoOperCountService;
import com.dd.cloud.user.service.resource.IPostInfoService;
import com.dd.cloud.user.service.resource.ISpreadInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author Lx
 * @version 1.0
 * @date 2023/10/20
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private IPostInfoService postInfoService;
    @Autowired
    private IPostInfoOperCountService postInfoOperCountService;
    @Autowired
    private IAdvertInfoService advertInfoService;
    @Autowired
    private ISpreadInfoService spreadInfoService;

    /**
     * 创建帖子/发帖 运营后台
     *
     * @param req
     */
    @PostMapping("/post/info/create/sys")
    public ResponseEntity<Void> createPostInfoSys(@Valid @RequestBody PostInfoCreateReq req) {
        postInfoService.createPostInfoSys(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改帖子 运营后台
     *
     * @param req
     */
    @PostMapping("/post/info/update/sys")
    public ResponseEntity<Void> updatePostInfoSys(@Valid @RequestBody PostInfoUpdateReq req) {
        postInfoService.updatePostInfoSys(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 置顶帖子
     *
     * @param id 帖子id
     */
    @PostMapping("/post/info/topping")
    public ResponseEntity<Void> toppingPostInfo(@NotNull(message = "帖子id不能为空") Integer id) {
        postInfoService.toppingPostInfo(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 取消置顶帖子
     *
     * @param id 帖子id
     */
    @PostMapping("/post/info/cancel/topping")
    public ResponseEntity<Void> cancelToppingPostInfo(@NotNull(message = "帖子id不能为空") Integer id) {
        postInfoService.cancelToppingPostInfo(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改帖子状态
     *
     * @param id     帖子id
     * @param status 帖子状态 1正常 2下架 -1删除
     */
    @PostMapping("/post/info/update/status/sys")
    public ResponseEntity<Void> updatePostInfoStatus(@NotNull(message = "帖子id不能为空") Integer id, @NotNull(message = "帖子状态不能为空") Integer status) {
        postInfoService.updatePostInfoStatus(id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询帖子列表-分页-运营后台
     *
     * @param req
     * @return
     */
    @GetMapping("/post/info/page/sys")
    public ResponseEntity<IPage<PostInfoPageSysRes>> queryPostInfoPageSys(@Valid PostInfoPageSysReq req) {
        IPage<PostInfoPageSysRes> res = postInfoService.queryPostInfoPageSys(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询帖子列表-分页-医生端
     *
     * @param req
     * @return
     */
    @GetMapping("/post/info/page")
    public ResponseEntity<IPage<PostInfoPageRes>> queryPostInfoPage(@Valid PostInfoPageReq req) {
        IPage<PostInfoPageRes> res = postInfoService.queryPostInfoPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询帖子详情-h5(目前可共用所有查询帖子详情数据)
     *
     * @param id 帖子id
     * @return
     */
    @GetMapping("/post/info/page/detail/h5")
    public ResponseEntity<PostInfoDetailH5Res> queryPostInfoDetailH5(@NotNull(message = "帖子id不能为空") Integer id) {
        PostInfoDetailH5Res res = postInfoService.queryPostInfoDetailH5(id);
        return ResponseEntity.ok(res);
    }

    /**
     * 帖子操作数量
     *
     * @param postInfoId 帖子id
     * @param type       数量类型 1阅读数 2点赞数 3转发数
     */
    @PostMapping("/post/info/oper/count")
    public ResponseEntity<Void> createPostInfoOperCount(@NotNull(message = "帖子id不能为空") Integer postInfoId, @NotNull(message = "数量类型不能为空") Integer type) {
        postInfoOperCountService.createPostInfoOperCount(postInfoId, type);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询广告弹框列表
     *
     * @param req
     * @return
     */
    @GetMapping("/advert/info/system/page")
    public ResponseEntity<IPage<AdvertInfoSystemPageRes>> queryAdvertInfoSystemPage(@Valid AdvertInfoSystemPageReq req) {
        IPage<AdvertInfoSystemPageRes> res = advertInfoService.queryAdvertInfoSystemPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 创建广告弹框
     *
     * @param req
     */
    @PostMapping("/advert/info/system/create")
    public ResponseEntity<Void> createAdvertInfoSystem(@RequestBody AdvertInfoSystemCreateReq req) {
        advertInfoService.createAdvertInfoSystem(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改广告弹框
     *
     * @param req
     */
    @PostMapping("/advert/info/system/update")
    public ResponseEntity<Void> updateAdvertInfoSystemUpdate(@RequestBody AdvertInfoSystemUpdateReq req) {
        advertInfoService.updateAdvertInfoSystemUpdate(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改广告弹框状态
     *
     * @param req
     */
    @PostMapping("/advert/info/system/update/status")
    public ResponseEntity<Void> updateAdvertInfoSystemUpdateStatus(@RequestBody @Valid AdvertInfoSystemUpdateStatusReq req) {
        advertInfoService.updateAdvertInfoSystemUpdateStatus(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询广告弹框详情
     *
     * @param id
     * @return
     */
    @GetMapping("/advert/info/system/detail")
    public ResponseEntity<AdvertInfoSystemDetailRes> queryAdvertInfoSystemDetail(@NotNull(message = "广告弹框id不能为空") Integer id) {
        AdvertInfoSystemDetailRes res = advertInfoService.queryAdvertInfoSystemDetail(id);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询开屏信息列表
     *
     * @param req
     * @return
     */
    @GetMapping("/spread/info/system/page")
    public ResponseEntity<IPage<SpreadInfoSystemPageRes>> querySpreadInfoSystemPage(@Valid SpreadInfoSystemPageReq req) {
        IPage<SpreadInfoSystemPageRes> res = spreadInfoService.querySpreadInfoSystemPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 创建开屏信息
     *
     * @param req
     */
    @PostMapping("/spread/info/system/create")
    public ResponseEntity<Void> createSpreadInfoSystem(@RequestBody SpreadInfoSystemCreateReq req) {
        spreadInfoService.createSpreadInfoSystem(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改开屏信息
     *
     * @param req
     */
    @PostMapping("/spread/info/system/update")
    public ResponseEntity<Void> updateSpreadInfoSystem(@RequestBody SpreadInfoSystemUpdateReq req) {
        spreadInfoService.updateSpreadInfoSystem(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 验证是否有开屏页面为显示状态
     *
     * @param id 排除id
     * @return true有开屏页面为显示状态 false没有开屏页面为显示状态
     */
    @GetMapping("/spread/info/system/verify")
    public ResponseEntity<Boolean> verifySpreadInfoIsShow(Integer id) {
        Boolean res = spreadInfoService.verifySpreadInfoIsShow(id);
        return ResponseEntity.ok(res);
    }

    /**
     * 修改开屏信息状态
     *
     * @param req
     */
    @PostMapping("/spread/info/system/update/status")
    public ResponseEntity<Void> updateSpreadInfoSystemUpdateStatus(@RequestBody @Valid SpreadInfoSystemUpdateStatusReq req) {
        spreadInfoService.updateSpreadInfoSystemUpdateStatus(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询开屏信息详情
     *
     * @param id
     * @return
     */
    @GetMapping("/spread/info/system/detail")
    public ResponseEntity<SpreadInfoSystemDetailRes> querySpreadInfoSystemDetail(@NotNull(message = "开屏信息id不能为空") Integer id) {
        SpreadInfoSystemDetailRes res = spreadInfoService.querySpreadInfoSystemDetail(id);
        return ResponseEntity.ok(res);
    }

    /**
     * 定时任务修改广告状态
     * 定时任务修改开屏页面状态
     */
    @PostMapping("/spread/advert/task/handle")
    public ResponseEntity<Void> taskUpdateStatus() {
        spreadInfoService.taskUpdateStatus();
        advertInfoService.taskUpdateStatus();
        return ResponseEntity.ok().build();
    }

    /**
     * 广告记录
     */
    @PostMapping("/add/advert/record")
    public ResponseEntity<Void> advertRecord(@NotNull(message = "id不能为空") Integer id) {
        spreadInfoService.advertRecord(id);
        return ResponseEntity.ok().build();
    }


    /**
     * 查询开屏
     */
    @GetMapping("/query/is/user")
    public ResponseEntity<Integer> queryIsUser(@RequestParam("id") Integer id) {
        Integer flag = spreadInfoService.queryIsUser(id);
        return ResponseEntity.ok(flag);
    }
}
